package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    ItemMapper itemMapper;


    @Override
    public PageDTO<Item> getlist(Integer page, Integer size) {
//        //1. 设置当前页与页面大小
//        Page<Item> pageF =new Page<>(page,size);
//
//        //2. 创建分页需要条件
//        QueryWrapper queryWrapper = new QueryWrapper();
//
//        List<Item> records = pageF.getRecords();
//        System.out.println("records = " + records);
//
//        long pages = pageF.getPages();
//        System.out.println("pages = " + pages);
//
//        //构建分页参数
//        getBaseMapper().selectPage(pageF, queryWrapper);


        Page page1 = page(new Page(page, size));


        //封装返回参数
        PageDTO<Item> objectPageDTO = new PageDTO<>();
        objectPageDTO.setTotal(page1.getPages());
        objectPageDTO.setList(page1.getRecords());
        return objectPageDTO;
    }

    @Override
    public Item getItem(Long id) {

        return itemMapper.selectById(id);
    }

    @Override
    public int addItem(Item item) {
        return itemMapper.insert(item);
    }


    //上下架
    @Override
    public void updateItemStatus(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);
    }

    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    public void deleteItem(Long id) {
        itemMapper.deleteById(id);
    }


}
